{% extends "layout/content.twig" %}

{% block main %}
    <div class="layui-card">
        <div class="layui-card-header">
            {{ __('general.header') }}
        </div>
        <div class="layui-card-body">
            <div class="layui-tab layui-tab-brief">
                <ul class="layui-tab-title">
                    <li class="layui-this">{{ __('login.title') }}</li>
                    <li>{{ __('site.title') }}</li>
                    <li>{{ __('smtp.title') }}</li>
                    <li class="">{{ __('upload.title') }}</li>
                    <li>{{ __('sms.title') }}</li>
                </ul>
                <div class="layui-tab-content">
                    <div class="layui-tab-item layui-show">
                        {% include 'general/config/login.twig' %}
                    </div>
                    <div class="layui-tab-item ">
                        {% include 'general/config/site.twig' %}
                    </div>
                    <div class="layui-tab-item">
                        {% include 'general/config/smtp.twig' %}
                    </div>
                    <div class="layui-tab-item ">
                        {% include 'general/config/upload.twig' %}
                    </div>
                    <div class="layui-tab-item">
                        {% include 'general/config/sms.twig' %}
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block script %}
    <script type="text/javascript">

        let save_config_url = '{{ auth_route('general/saveSetting') }}'

        layui.use(['http', 'toast', 'form', 'upload'], function () {

            let http = layui.http
            let form = layui.form
            let toast = layui.toast
            let upload = layui.upload

            form.on('submit(login)', function (d) {
                let support = []
                $('[name=support]').each(function () {
                    if ($(this).prop('checked')) {
                        support.push($(this).val())
                    }
                })

                let fields = d.field
                fields['support'] = support

                http.post(save_config_url,
                    {
                        'group': 'login',
                        setting: fields,
                    })
                    .then(res => {
                        toast.top(res)
                    })

                return false
            })

            form.on('submit(site)', function (d) {
                let fields = d.field
                http.post(save_config_url,
                    {
                        'group': 'site',
                        setting: fields,
                    })
                    .then(res => {
                        toast.top(res)
                    })
                return false
            })

            form.on('submit(smtp)', function (d) {
                let fields = d.field
                http.post(save_config_url,
                    {
                        'group': 'smtp',
                        setting: fields,
                    })
                    .then(res => {
                        toast.top(res)
                    })
                return false
            })

            form.on('submit(upload)', function (d) {
                let fields = d.field
                let mime = []
                $('[name=mime]').each(function () {
                    if ($(this).prop('checked')) {
                        mime.push($(this).val())
                    }
                })
                fields['mime'] = mime
                http.post(save_config_url,
                    {
                        'group': 'upload',
                        setting: fields,
                    })
                    .then(res => {
                        toast.top(res)
                    })
                return false
            })

            form.on('submit(sms)', function (d) {
                let fields = d.field
                http.post(save_config_url,
                    {
                        'group': 'sms',
                        setting: fields,
                    })
                    .then(res => {
                        toast.top(res)
                    })
                return false
            })

            upload.render({
                elem: '#site_icon',
                url: '{{ auth_route('upload') }}',
                done: function (res) {
                    if (res.code) {
                        toast.error(res.msg)
                    } else {
                        $('[name=icon]').val(res.data.url)
                    }
                },
            })

            upload.render({
                elem: '#site_logo',
                url: '{{ auth_route('upload/') }}',
                done: function (res) {
                    if (res.code) {
                        toast.error(res.msg)
                    } else {
                        $('[name=logo]').val(res.data.url)
                    }
                },
            })

            $('#email-send-test').on('click', function () {

                let email = $('[name=email_receive]').val()

                http.post('{{ auth_route('general/emailTest') }}',
                    {
                        email: email,
                    })
                    .then(res => {
                        toast.top(res)
                    })
                return false
            })
        })
    </script>
{% endblock %}